import request from '@/utils/request'

const baseURL = '/admin/user'

// 登录接口
export const login = (adminName, password) => {
    return request.post(`${baseURL}/login`, null, {
        params: {
            adminName,
            password
        }
    })
}

// 获取用户信息
export const getUserInfo = () => {
    return request.get(`${baseURL}/info`)
}

// 获取用户列表
export const getUserList = (params) => {
    return request.get(`${baseURL}/list`, { params })
}

// 获取用户详情
export const getUserDetail = (id) => {
    return request.get(`${baseURL}/detail`, { params: { id } })
}

// 新增用户
export const addUser = (data) => {
    const formData = new FormData()
    Object.keys(data).forEach(key => {
        if (data[key] !== null && data[key] !== undefined) {
            formData.append(key, data[key])
        }
    })
    return request.post(`${baseURL}/add`, formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 更新用户
export const updateUser = (data) => {
    const formData = new FormData()
    Object.keys(data).forEach(key => {
        if (data[key] !== null && data[key] !== undefined) {
            formData.append(key, data[key])
        }
    })
    return request.post(`${baseURL}/update`, formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 删除用户
export const deleteUser = (id) => {
    return request.delete(`${baseURL}/delete`, { params: { id } })
}

// 上传头像
export const uploadAvatar = (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return request.post(`${baseURL}/upload/avatar`, formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
} 